package com.factory.manager.pojo;

import com.factory.common.bean.User;
import com.factory.common.constant.UserConstant;
import com.factory.common.pojo.Result;
import com.factory.common.utils.StringUtil;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.Optional;

public class UserPojo extends User {

    @Override
    protected Optional<Result> vInsert() {

        if (StringUtil.isEmpty(getAccount())) {
            return Optional.of(Result.fail("account can not be empty!"));
        } else if (StringUtil.isEmpty(getPassword())) {
            return Optional.of(Result.fail("password can not be empty!"));
        }

        setId(StringUtil.getUUID());
        setStatus(UserConstant.Status.NORMAL);
        setCreateDate(new Date());
        setPassword(DigestUtils.md5DigestAsHex(StringUtil.val(getPassword(), getAccount()).getBytes()));

        return super.vInsert();
    }

    @Override
    protected Optional<Result> vDelete() {
        return super.vDelete();
    }

    @Override
    protected Optional<Result> vUpdate() {

        if (StringUtil.isEmpty(getId())) {
            return Optional.of(Result.fail("account can not be empty!"));
        } else if (StringUtil.notEmpty(getPassword())) {
            setPassword(DigestUtils.md5DigestAsHex(getPassword().getBytes()));
        } else if (StringUtil.isEmpty(getPassword())) {
            //将空字符串替换为空值
            setPassword(null);
        }

        return super.vUpdate();
    }

    @Override
    protected Optional<Result> vQuery() {
        return super.vQuery();
    }

    @Override
    protected Optional<Result> vQueryList() {
        return super.vQueryList();
    }
}
